home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / mosml / src / mosmllib / Array.sig < prev    next >
Encoding:
Text File  |  1996-07-03  |  6.0 KB  |  146 lines  |  [TEXT/R*ch]

  1. (* Array -- SML Standard Library *)
  2.  
  3. prim_EQtype 'a array
  4.  
  5. val maxLen   : int
  6.  
  7. val array    : int * '_a -> '_a array
  8. val tabulate : int * (int -> '_a) -> '_a array
  9. val fromList : '_a list -> '_a array
  10. val array0   : 'a array    
  11.  
  12. val length   : 'a array -> int
  13. val sub      : 'a array * int -> 'a
  14. val update   : 'a array * int * 'a  -> unit
  15. val extract  : 'a array * int * int option -> 'a Vector.vector
  16.  
  17. val copy     : {src: 'a array,  si: int, dst: 'a array, di: int, len: int}
  18.                -> unit
  19. val copyv    : {src: 'a vector, si: int, dst: 'a array, di: int, len: int}
  20.                -> unit
  21.  
  22. val app      : ('a -> unit) -> 'a array -> unit
  23. val foldl    : ('a * 'b -> 'b) -> 'b -> 'a array -> 'b
  24. val foldr    : ('a * 'b -> 'b) -> 'b -> 'a array -> 'b
  25. val modify   : ('a -> 'a) -> 'a array -> unit
  26.  
  27. val appi     : (int * 'a -> unit) -> 'a array * int * int option -> unit
  28. val foldli   : (int * 'a * 'b -> 'b) -> 'b -> 'a array * int * int option -> 'b
  29. val foldri   : (int * 'a * 'b -> 'b) -> 'b -> 'a array * int * int option -> 'b
  30. val modifyi  : (int * 'a -> 'a) -> 'a array * int * int option -> unit
  31.  
  32. (* Type [ty array] is the type of one-dimensional, mutable, zero-based
  33.    constant-time-access arrays with elements of type ty.  Type ty
  34.    array admits equality even if ty does not.  Arrays a1 and a2 are
  35.    equal if both were created by the same call to a primitive (array,
  36.    tabulate, fromList, array0).
  37.  
  38.    [maxLen] is the maximal number of elements in an array.
  39.  
  40.    [array(n, x)] returns a new array of length n whose elements are all x.
  41.    Raises Size if n<0 or n>maxLen.
  42.  
  43.    [tabulate(n, f)] returns a new array of length n whose elements
  44.    are f 0, f 1, ..., f (n-1), created from left to right.  Raises
  45.    Size if n<0 or n>maxLen.
  46.  
  47.    [fromList xs] returns an array whose elements are those of xs.
  48.    Raises Size if length xs > maxLen.
  49.  
  50.    [array0] is a zero-length array.
  51.  
  52.    [length a] returns the number of elements in a.
  53.  
  54.    [sub(a, i)] returns the i'th element of a, counting from 0.  
  55.    Raises Subscript if i<0 or i>=length a.  To make `sub' infix, use
  56.    the declaration 
  57.                  infix 9 sub
  58.  
  59.    [update(a, i, x)] destructively replaces the i'th element of a by x.
  60.    Raises Subscript if i<0 or i>=length a.
  61.  
  62.    [extract(a, i, NONE)] returns a vector of the elements a[i..length a-1] 
  63.    of a.  Raises Subscript if i<0 or i>length a.
  64.  
  65.    [extract(a, i, SOME len)] returns a vector of the elements a[i..i+len-1] 
  66.    of a.  Raises Subscript if i<0 or len<0 or i+len>length a or
  67.    len>Vector.maxLen.
  68.  
  69.    [copy{src, si, dst, di, len}] destructively copies
  70.    src[si..si+len-1] to dst[di..di+len-1], and works also if src and
  71.    dst are the same and the segments overlap.  Raises Subscript if
  72.    len<0 or si<0 or si+len>length src or di<0 or di+len>length dst.
  73.  
  74.    [copyv{src, si, dst, di, len}] destructively copies
  75.    src[si..si+len-1] to dst[di..di+len-1].  Raises Subscript if
  76.    len<0 or si<0 or si+len>length src or di<0 or di+len>length dst.
  77.  
  78.    [foldl f e a] folds function f over a from left to right.  That is,
  79.    computes f(a[len-1], f(a[len-2], ..., f(a[1], f(a[0], e)) ...)),
  80.    where len is the length of a.
  81.  
  82.    [foldr f e a] folds function f over a from right to left.  That is,
  83.    computes f(a[0], f(a[1], ..., f(a[len-2], f(a[len-1], e)) ...)),
  84.    where len is the length of a.
  85.  
  86.    [app f a] applies f to a[j] for j=0,1,...,length a-1.
  87.  
  88.    [modify f a] applies f to a[j] and updates a[j] with the result
  89.    f(a[j]) for j=0,1,...,length a-1. 
  90.  
  91.    The following iterators generalize the above ones in two ways:
  92.  
  93.     . the index j is also being passed to the function being iterated;
  94.     . the iterators work on a slice (subarray) of an array.
  95.  
  96.    The slice (a, i, SOME n) denotes the subarray a[i..i+n-1].  That is,
  97.    a[i] is the first element of the slice, and n is the length of the
  98.    slice.  Valid only if 0 <= i <= i+n <= length a.
  99.  
  100.    The slice (a, i, NONE) denotes the subarray a[i..length a-1].  That
  101.    is, the slice denotes the suffix of the array starting at i.  Valid
  102.    only if 0 <= i <= length a.  Equivalent to (a, i, SOME(length a - i)).
  103.  
  104.        slice             meaning 
  105.        ----------------------------------------------------------
  106.        (a, 0, NONE)      the whole array              a[0..len-1]   
  107.        (a, 0, SOME n)    a left subarray (prefix)     a[0..n-1]
  108.        (a, i, NONE)      a right subarray (suffix)    a[i..len-1]
  109.        (a, i, SOME n)    a general slice              a[i..i+n-1] 
  110.  
  111.    [foldli f e (a, i, SOME n)] folds function f over the subarray
  112.    a[i..i+n-1] from left to right.  That is, computes 
  113.    f(i+n-1, a[i+n-1], f(..., f(i+1, a[i+1], f(i, a[i], e)) ...)).  
  114.    Raises Subscript if i<0 or n<0 or i+n > length a.
  115.  
  116.    [foldli f e (a, i, NONE)] folds function f over the subarray
  117.    a[i..len-1] from left to right, where len =  length a.  That is, 
  118.    computes f(len-1, a[len-1], f(..., f(i+1, a[i+1], f(i, a[i], e)) ...)).  
  119.    Raises Subscript if i<0 or i > length a.
  120.  
  121.    [foldri f e (a, i, SOME n)] folds function f over the subarray
  122.    a[i..i+n-1] from right to left.  That is, computes 
  123.    f(i, a[i], f(i+1, a[i+1], ..., f(i+n-1, a[i+n-1], e) ...)).
  124.    Raises Subscript if i<0 or n<0 or i+n > length a.
  125.  
  126.    [foldri f e (a, i, NONE)] folds function f over the subarray
  127.    a[i..len-1] from right to left, where len = length a.  That is, 
  128.    computes f(i, a[i], f(i+1, a[i+1], ..., f(len-1, a[len-1], e) ...)).
  129.    Raises Subscript if i<0 or i > length a.
  130.  
  131.    [appi f (a, i, SOME n)] applies f to successive pairs (j, a[j]) for
  132.    j=i,i+1,...,i+n-1.  Raises Subscript if i<0 or n<0 or i+n > length a.
  133.  
  134.    [appi f (a, i, NONE)] applies f to successive pairs (j, a[j]) for
  135.    j=i,i+1,...,len-1, where len = length a.  Raises Subscript if i<0
  136.    or i > length a.
  137.  
  138.    [modifyi f (a, i, SOME n)] applies f to (j, a[j]) and updates a[j]
  139.    with the result f(j, a[j]) for j=i,i+1,...,i+n-1.  Raises Subscript
  140.    if i<0 or n<0 or i+n > length a.
  141.  
  142.    [modifyi f (a, i, NONE)] applies f to (j, a[j]) and updates a[j]
  143.    with the result f(j, a[j]) for j=i,i+1,...,len-1.  Raises Subscript
  144.    if i<0 or i > length a.
  145. *)
  146.